<?php

namespace App\Models\Product;

use App\Libraries\Decorator\Product\VIPDecorator;
use App\Models\BaseModel;
use App\Models\ItemModel;
use App\Models\User\Address;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class Product extends BaseModel
{


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'created_at','updated_at','cover_id'
    ];

    protected $primaryKey = 'product_id';
    protected $dateFormat = 'U';

    /**
     * 获取允许兑换的商品数据
     * @param $page
     * @param $size
     * @param $category
     * @param $type
     * @return mixed
     */
    public function sellingProducts($page, $size, $category, $type){
        $data = [];
        $this->addDecorator(new VIPDecorator());
        $data['list'] = $this->with('content')
            ->where('status',1)
            ->where('stock', '>=', 1)
            ->where('category', $category)
            ->where('money', '>', 0)
            ->whereIn('type', [0, $type])
            ->whereRaw("(CASE WHEN(category<>2) THEN validity ELSE 99999999999 END)>=".time())
            ->orderBy('priority', 'desc')
            ->forPage($page, $size+1)
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;

        return $this->after($data);
    }

    /**
     * 获取商品详细数据
     * @param $id
     * @return mixed
     */
    public function detail($id){
        $data = $this->with('content')
            ->where('status',1)
            ->where('stock', '>=', 1)
            ->where('product_id', $id)
            ->where('money', '>', 0)
            ->firstOrFail()
            ->toArray();
        return $this->after($data);
    }

    /**
     * 购买商品前检查
     * @param Request $request
     * @param Address $address
     * @return array
     */
    public function beforeCheck(Request &$request, Address $address){
        $user = Auth::user();
        $this->addDecorator(new VIPDecorator());
        $product = $this->where('status',1)
            ->where('stock', '>=', 1)
            ->where('product_id', $request['id'])
            ->where('money', '>', 0)
            ->whereRaw("(CASE WHEN(category<>2) THEN validity ELSE 99999999999 END)>=".time())
            ->firstOrFail()
            ->toArray();
        $product = $this->after($product);
        if($product['category'] == 2){
            $request['address_id'] = 0;
        }else{
            if(empty($request['address_id'])){
                $this->rs_format['code'] = 10001;
                return $this->rs_format;
            }
            $address = $address->where('id', $request['address_id'])->firstOrFail();
            if($address->user_id != $user->user_id){
                $this->rs_format['code'] = 40001;
                return $this->rs_format;
            }
            $flag = $this->where('product_id', $request['id'])
                ->where('stock', '>=', 1)
                ->decrement('stock');
            if(!$flag){
                $this->rs_format['code'] = 50002;
                return $this->rs_format;
            }
        }
        $this->rs_format['data']['price'] = $product['money'];
        $this->rs_format['data']['body'] = $product['title'];
        $this->rs_format['data']['ext_id'] = $request['address_id'];
        return $this->rs_format;
    }

    /**
     * 关联product_content
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function content(){
        return $this->hasOne('App\Models\Product\ProductContent', 'product_id', 'product_id');
    }
}
